package com.ccit.paas.rpc.vo;

import com.ccit.paas.rpc.enums.ErrorCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 响应前端结果通用封装类
 *
 * @author:
 * @date 2024/1/9 15:58
 * @version: v1.0 初始化
 */
@Data
public class ResponseVO<T> implements Serializable {

    private Integer code;

    private T data;

    private String msg;

    public ResponseVO() {
    }

    public ResponseVO(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResponseVO(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ResponseVO<T> success() {
        ResponseVO<T> vo = new ResponseVO<>();
        vo.setCode(ErrorCode.SUCCESS.getCode());
        vo.setMsg(ErrorCode.SUCCESS.getMessage());
        return vo;
    }

    public static <T> ResponseVO<T> success(T data) {
        ResponseVO<T> vo = new ResponseVO<>();
        vo.setCode(ErrorCode.SUCCESS.getCode());
        vo.setMsg(ErrorCode.SUCCESS.getMessage());
        vo.setData(data);
        return vo;
    }

    public static <T> ResponseVO<T> fail(ErrorCode errorCode) {
        ResponseVO<T> vo = new ResponseVO<>();
        vo.setCode(errorCode.getCode());
        vo.setMsg(errorCode.getMessage());
        return vo;
    }

    public static <T> ResponseVO<T> fail(int code, String msg) {
        ResponseVO<T> vo = new ResponseVO<>();
        vo.setCode(code);
        vo.setMsg(msg);
        return vo;
    }
}
